package com.sims.daoImp;

import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.sims.bean.CourseChart;
import com.sims.dao.ICourseChartDAO;

/**
 * A data access object (DAO) providing persistence and search support for
 * CourseChart entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.sims.bean.CourseChart
 * @author MyEclipse Persistence Tools
 */

public class CourseChartDAO extends HibernateDaoSupport implements ICourseChartDAO {
	private static final Logger log = LoggerFactory
			.getLogger(CourseChartDAO.class);
	// property constants
	public static final String CID = "cid";
	public static final String WEEKDAY = "weekday";
	public static final String NO = "no";
	public static final String COURSE = "course";

	@Override
	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.ICourseChartDAO#save(com.sims.bean.CourseChart)
	 */
	@Override
	public void save(CourseChart transientInstance) {
		log.debug("saving CourseChart instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.ICourseChartDAO#delete(com.sims.bean.CourseChart)
	 */
	@Override
	public void delete(CourseChart persistentInstance) {
		log.debug("deleting CourseChart instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.ICourseChartDAO#findById(java.lang.Integer)
	 */
	@Override
	public CourseChart findById(java.lang.Integer id) {
		log.debug("getting CourseChart instance with id: " + id);
		try {
			CourseChart instance = (CourseChart) getHibernateTemplate().get(
					"com.sims.bean.CourseChart", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.ICourseChartDAO#findByExample(com.sims.bean.CourseChart)
	 */
	@Override
	public List findByExample(CourseChart instance) {
		log.debug("finding CourseChart instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.ICourseChartDAO#findByProperty(java.lang.String, java.lang.Object)
	 */
	@Override
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding CourseChart instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from CourseChart as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.ICourseChartDAO#findByCid(java.lang.Object)
	 */
	@Override
	public List findByCid(Object cid) {
		return findByProperty(CID, cid);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.ICourseChartDAO#findByWeekday(java.lang.Object)
	 */
	@Override
	public List findByWeekday(Object weekday) {
		return findByProperty(WEEKDAY, weekday);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.ICourseChartDAO#findByNo(java.lang.Object)
	 */
	@Override
	public List findByNo(Object no) {
		return findByProperty(NO, no);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.ICourseChartDAO#findByCourse(java.lang.Object)
	 */
	@Override
	public List findByCourse(Object course) {
		return findByProperty(COURSE, course);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.ICourseChartDAO#findAll()
	 */
	@Override
	public List findAll() {
		log.debug("finding all CourseChart instances");
		try {
			String queryString = "from CourseChart";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.ICourseChartDAO#merge(com.sims.bean.CourseChart)
	 */
	@Override
	public CourseChart merge(CourseChart detachedInstance) {
		log.debug("merging CourseChart instance");
		try {
			CourseChart result = getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.ICourseChartDAO#attachDirty(com.sims.bean.CourseChart)
	 */
	@Override
	public void attachDirty(CourseChart instance) {
		log.debug("attaching dirty CourseChart instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.ICourseChartDAO#attachClean(com.sims.bean.CourseChart)
	 */
	@Override
	public void attachClean(CourseChart instance) {
		log.debug("attaching clean CourseChart instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static ICourseChartDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (ICourseChartDAO) ctx.getBean("CourseChartDAO");
	}
}